<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="dialogVisible = true"
          >添加分类</el-button
        >
      </div>
      <!-- 表格区域 -->
      <el-table style="width: 100%" :data="list" border stripe>
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="分类名称" prop="cate_name"></el-table-column>
        <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 传入值 点击执行函数 -->
          <template #default="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="getArtCateById(row.id)"
              >修改</el-button
            >
            <el-button type="danger" size="mini" @click="delArtCate(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增文章分类的弹层 -->
    <el-dialog
      @close="resetForm"
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="30%"
    >
      <span>
        <!-- 表单收集数据 -->
        <el-form
          :model="releForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="20%"
          class="demo-dynamic"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="releForm.cate_name" />
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="releForm.cate_alias" />
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArtCate">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改文章分类的弹层 -->
    <el-dialog title="修改文章分类" :visible.sync="uDialogVisible" width="30%">
      <span>
        <!-- 表单收集数据 -->
        <el-form
          :model="uReleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="20%"
          class="demo-dynamic"
        >
          <el-form-item label="分类名称" prop="cate_name">
            <el-input v-model="uReleForm.cate_name" />
          </el-form-item>
          <el-form-item label="分类别名" prop="cate_alias">
            <el-input v-model="uReleForm.cate_alias" />
          </el-form-item>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="uDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updataArtCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "ArtCate",
  created() {
    // 页面开始调用函数 初始化数据
    this.getArtCateList();
  },
  data() {
    return {
      // 表单收集数据对象
      releForm: {
        cate_name: "",
        cate_alias: "",
      },
      // 收集编辑文章分类的数据对象
      uReleForm: {
        cate_name: "",
        cate_alias: "",
      },
      // 验证规则对象
      rules: {
        cate_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          {
            pattern: /^\S{1,10}$/,
            message: "分类名必须是1-10位的非空字符",
            trigger: "blur",
          },
        ],
        cate_alias: [
          { required: true, message: "请输入分类别名", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: "分类别名必须是1-15位的字母数字",
            trigger: "blur",
          },
        ],
      },
      // 保存的是文章分类列表
      list: [],
      dialogVisible: false, //控制添加弹层的显示隐藏
      uDialogVisible: false, //控制修改弹层的显示隐藏
    };
  },
  methods: {
    // 获取文章分类的详细信息
    async getArtCateById(id) {
      const { data: res } = await this.$http.get("/my/cate/info", {
        params: { id },
      });
      if (res.code !== 0) return;
      console.log(1);
      this.uReleForm = res.data;
      this.uDialogVisible = true;
    },
    // 添加数据
    addArtCate() {
      // 1. 绑定事件函数
      // 2. 兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return; // 校验失败
        // 3. 发送请求实现添加
        const { data: res } = await this.$http.post(
          "/my/cate/add",
          this.releForm
        );
        if (res.code !== 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        //  4. 更新数据
        this.getArtCateList();
        //  5. 关闭弹窗
        this.dialogVisible = false;
      });
    },
    // 修改数据
    updataArtCate() {
      // 1. 绑定事件函数
      // 2. 兜底校验
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return; // 校验失败
        // 3. 发送请求实现添加
        const { data: res } = await this.$http.put(
          "/my/cate/info",
          this.uReleForm
        );
        if (res.code !== 0) return this.$message.error(res.message);
        this.$message.success(res.message);
        //  4. 更新数据
        this.getArtCateList();
        //  5. 关闭弹窗
        this.uDialogVisible = false;
      });
    },
    // 删除数据
    delArtCate(id) {
      // 1. 绑定事件函数
      // 2. 提示询问框
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          // 3. 发送请求删除数据
          const { data: res } = await this.$http.delete("/my/cate/del", {
            params: { id },
          });
          if (res.code !== 0) return this.$message.error(res.message);
          this.$message.success(res.message);
          // 4. 重新渲染
          this.getArtCateList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 重置表单
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    // 发送请求获取文章分类数据
    async getArtCateList() {
      const { data: res } = await this.$http.get("/my/cate/list");
      if (res.code !== 0) return this.$message.error(res.message);
      this.list = res.data;
    },
  },
};
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>